package com.etc.service.impl;

import com.etc.dao.MailMapper;
import com.etc.pojo.mail.Mail;
import com.etc.service.MailSerivce;
import com.etc.utils.MailUtils;
import com.etc.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class MailServiceImpl implements MailSerivce {

    @Autowired
    private MailMapper mailMapper;

    @Override
    public Integer insertMail(Mail mail) {
        return mailMapper.insertMail(mail);
    }

    @Override
    public Integer send(String message, String active, String touser) {


        try {
            MailUtils.sendMail(touser,active,message);
            //保存数据库
            Mail mail = new Mail();
            Date date = new Date();
            mail.setMessage(message);
            mail.setActive(active);
            mail.setTouser(touser);
            mail.setCreatetime(TimeUtils.DateToStr(date));
            mail.setUser("杰哥");
            Integer integer = this.insertMail(mail);
            if(integer == 1){
                return 1;
            }else{
                return 2;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return 2;
    }

    @Override
    public Integer deleteMail(int mid) {
        return mailMapper.deleteMail(mid);
    }

    @Override
    public List<Mail> mailAll() {
        return mailMapper.mailAll();
    }
}
